package cn.edu.xmu.software.ijoker.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import cn.edu.xmu.software.ijoker.R;
import cn.edu.xmu.software.ijoker.entity.Joke;

public class PlayService extends Service {
	public final String TAG = PlayService.class.getName();
	private MediaPlayer mp = new MediaPlayer();
	private List<String> jokeList = new ArrayList<String>();
	private Joke joke;
	private int currentPosition;
	private NotificationManager nm;
	private static final int NOTIFY_ID = R.layout.player;

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	private void playJoke(String file) {
		Log.i("playJoke:", file);
		try {

			Notification notification = new Notification();
			nm.notify(NOTIFY_ID, notification);
			Log.i("playJoke:", "------------------------");
			mp.reset();
			mp.setDataSource(file);
			mp.prepare();
			mp.start();
			mp.setOnCompletionListener(new OnCompletionListener() {
				public void onCompletion(MediaPlayer arg0) {
					nextJoke();
				}
			});
		} catch (IOException e) {
			Log.e(getString(R.string.app_name), e.getMessage());
		}
	}

	private void nextJoke() {
		// Check if last song or not
		if (++currentPosition >= jokeList.size()) {
			currentPosition = 0;
			nm.cancel(NOTIFY_ID);
		} else {
			playJoke(jokeList.get(currentPosition));
		}
	}

	private void preJoke() {
		if (mp.getCurrentPosition() < 3000 && currentPosition >= 1) {
			playJoke(jokeList.get(--currentPosition));
		} else {
			playJoke(jokeList.get(currentPosition));
		}
	}

	private final IPlayService.Stub mBinder = new IPlayService.Stub() {

		@Override
		public void play(String location) throws RemoteException {
			Log.i(TAG, "play file: " + location);
			try {
				// currentPosition = position;
				playJoke(location);
			} catch (IndexOutOfBoundsException e) {
				Log.e(TAG, e.getMessage());
			}
		}

		@Override
		public void pause() throws RemoteException {
			Notification notification = new Notification();
			nm.notify(NOTIFY_ID, notification);
			mp.pause();
		}

		@Override
		public void next() throws RemoteException {
			// Check if it is last joke or not
			nextJoke();
		}

		@Override
		public void pre() throws RemoteException {
			preJoke();
		}

		@Override
		public void addJokeList(String location) throws RemoteException {
			jokeList.add(location);

		}

		@Override
		public void clearJokeList() throws RemoteException {
			jokeList.clear();

		}

		@Override
		public void stop() throws RemoteException {
			nm.cancel(NOTIFY_ID);
			mp.stop();

		}
	};

	public void onCreate() {
		super.onCreate();
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	public void onDestroy() {
		super.onDestroy();
		mp.stop();
		mp.release();
		nm.cancel(NOTIFY_ID);
	}

}
